package org.lee.bmh;

import org.openjdk.jmh.annotations.*;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * DoublingNumber
 *
 * @author dralee
 * @date 2022-01-24 14:11:43
 **/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread)
@Fork(value = 2, jvmArgs = {"-Xms4G", "-Xmx4G"}) // 4G
public class DoublingNumber {
    public int doubleInt(int n) {
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return n * 2;
    }

    @Benchmark
    public int doubleAndSumSequential() {
        return IntStream.of(1, 5, 3, 9, 2, 6, 4).map(this::doubleInt).sum();
    }

    @Benchmark
    public int doubleAndSumParallel() {
        return IntStream.of(1, 5, 3, 9, 2, 6, 4).parallel().map(this::doubleInt).sum();
    }
}
